	#!/bin/bash
# ======================== package dir  ========================
TODAY=$(date +%Y%m%d);
if [ ! -d "/opt/build/$TODAY" ];then
	mkdir /opt/build/$TODAY;
	chmod -R 777 /opt/build/$TODAY;
else
	chmod -R 777 /opt/build/$TODAY;
fi
USER=${1};
PACKAGEVERSION=${2};
VERSIONUIC=${3};
VERSIONPLATFORM=${4};
VERSIONCAS=${5};
VERSIONSCORE=${6};
VERSIONTIMETABLE=${7};
VERSIONMICROSITE=${8};
VERSIONPORTAL=${9};
VERSIONOFFICE=${10};
VERSIONSTATIC=${11};
CURRENTTIME="$(date +%Y%m%d%k%M%S)";
echo $CURRENTTIME -----------start bxn update package version: $PACKAGEVERSION----------;
# ======================== fun getTime  ========================

export JAVA_HOME=/usr/java/jdk1.7.0_79;
export M2_HOME=/usr/local/maven;


UPDATEBASE=/tmp/update/bxn-update-base;
UPDATEZIP="bxn-update-base-$PACKAGEVERSION-$CURRENTTIME-$USER.zip";
echo package zip file is $UPDATEZIP;
echo tmp package base dir:$UPDATEBASE;
if [ -d "#UPDATEBASE" ]; then
	rm -rf $UPDATEBASE;
	echo $UPDATEBASE is exits, deleted.;
fi
export LC_CTYPE=en_US.UTF-8;
echo mkdirs;
mkdir -p $UPDATEBASE/bx_app/data/update/$PACKAGEVERSION;
touch $UPDATEBASE/bx_app/data/update/$PACKAGEVERSION/update.bat;
mkdir -p $UPDATEBASE/bx_app/program/nginx/nginx-1.7.0/conf/module;
mkdir -p $UPDATEBASE/bx_app/program/tomcat7pf/webapps;
mkdir -p $UPDATEBASE/bx_app/program/tomcat7apps1/webapps;
mkdir -p $UPDATEBASE/bx_app/program/tomcat7apps2/webapps;
mkdir -p $UPDATEBASE/bx_app/program/static;
mkdir -p $UPDATEBASE/bx_app/installer;


VERSIONALL=$UPDATEBASE/bx_app/config/version-$PACKAGEVERSION.properties;
echo write versions to $VERSIONALL;
if [ ! -f "$VERSIONALL" ];then
	touch $VERSIONALL;
fi
echo version file is $VERSIONALL;
echo "bxn-integration-uic="$VERSIONUIC  >> $VERSIONALL;
echo "bxn-core-platform="$VERSIONPLATFORM  >> $VERSIONALL;
echo "cas-server="$VERSIONCAS  >> $VERSIONALL;
echo "bxn-score="$VERSIONSCORE  >> $VERSIONALL;
echo "bxn-timetable="$VERSIONTIMETABLE  >> $VERSIONALL;
echo "bxn-microsite="$VERSIONMICROSITE  >> $VERSIONALL;
echo "bxn-portal="$VERSIONPORTAL  >> $VERSIONALL;
echo "bxn-integration-office="$VERSIONOFFICE  >> $VERSIONALL;
echo "bxn-static-resource="$VERSIONSTATIC  >> $VERSIONALL;

sh /usr/packagetools/bxn-update-download.sh   svnv6 bxn-integration-uic    release $VERSIONUIC						$PACKAGEVERSION;
sh /usr/packagetools/bxn-update-download.sh   svnv6 bxn-core-platform      release $VERSIONPLATFORM					$PACKAGEVERSION;
sh /usr/packagetools/bxn-update-download.sh   svnv6 cas-server             release $VERSIONCAS/cas-server-webapp	$PACKAGEVERSION;
sh /usr/packagetools/bxn-update-download.sh   svnv6 bxn-score              release $VERSIONSCORE					$PACKAGEVERSION;
sh /usr/packagetools/bxn-update-download.sh   svnv6 bxn-timetable          release $VERSIONTIMETABLE				$PACKAGEVERSION;
sh /usr/packagetools/bxn-update-download.sh   svnv6 bxn-microsite          release $VERSIONMICROSITE				$PACKAGEVERSION;
sh /usr/packagetools/bxn-update-download.sh   svnv6 bxn-portal             release $VERSIONPORTAL					$PACKAGEVERSION;
sh /usr/packagetools/bxn-update-download.sh   svnv6 bxn-integration-office release $VERSIONOFFICE					$PACKAGEVERSION;
sh /usr/packagetools/bxn-update-download.sh   svnv6 bxn-static-resource    release $VERSIONSTATIC					$PACKAGEVERSION;
# mysql data import
echo "write update.bat";
DATAUPDATEPATH=$UPDATEBASE/bx_app/data/update/$PACKAGEVERSION;
DATAUPDATEBAT=$UPDATEBASE/bx_app/data/update/$PACKAGEVERSION/update.bat;
folderlist=`ls $DATAUPDATEPATH`;
for folder in $folderlist
do 
	if [ -d "$DATAUPDATEPATH/$folder" ];then 
		echo scan $DATAUPDATEPATH/$folder;
		if [ -f "$DATAUPDATEPATH/$folder/alter_portal.sql" ];then 
			echo "found sql :$DATAUPDATEPATH/$folder/alter_portal.sql";
			echo "mysql -h127.0.0.1 -ubxn -p{bxnPassword} --default-character-set=utf8 < \"{BX_HOME}\\data\\update\\$PACKAGEVERSION\\$folder\\alter_portal.sql\"" >> $DATAUPDATEBAT;
		fi
		if [ -f "$DATAUPDATEPATH/$folder/alter_data.sql" ];then 
			echo "found sql :$DATAUPDATEPATH/$folder/alter_data.sql";
			echo "mysql -h127.0.0.1 -ubxn -p{bxnPassword} --default-character-set=utf8 < \"{BX_HOME}\\data\\update\\$PACKAGEVERSION\\$folder\\alter_data.sql\"" >> $DATAUPDATEBAT;
		fi
		if [ -f "$DATAUPDATEPATH/$folder/alter_bxn.sql" ];then 
			echo "found sql :$DATAUPDATEPATH/$folder/alter_bxn.sql";
			echo "mysql -h127.0.0.1 -ubxn -p{bxnPassword} --default-character-set=utf8 < \"{BX_HOME}\\data\\update\\$PACKAGEVERSION\\$folder\\alter_bxn.sql\"" >> $DATAUPDATEBAT;
		fi
		if [ -f "$DATAUPDATEPATH/$folder/alter_table.sql" ];then 
			echo "found sql :$DATAUPDATEPATH/$folder/alter_table.sql";
			echo "mysql -h127.0.0.1 -ubxn -p{bxnPassword} --default-character-set=utf8 < \"{BX_HOME}\\data\\update\\$PACKAGEVERSION\\$folder\\alter_table.sql\"" >> $DATAUPDATEBAT;
		fi
		if [ -f "$DATAUPDATEPATH/$folder/alter_activemq.sql" ];then 
			echo "found sql :$DATAUPDATEPATH/$folder/alter_activemq.sql";
			echo "mysql -h127.0.0.1 -ubxn -p{bxnPassword} --default-character-set=utf8 < \"{BX_HOME}\\data\\update\\$PACKAGEVERSION\\$folder\\alter_activemq.sql\"" >> $DATAUPDATEBAT;
		fi
		if [ -f "$DATAUPDATEPATH/$folder/rebuild_index.sql" ];then 
			echo "found sql :$DATAUPDATEPATH/$folder/rebuild_index.sql";
			echo "mysql -h127.0.0.1 -ubxn -p{bxnPassword} --default-character-set=utf8 < \"{BX_HOME}\\data\\update\\$PACKAGEVERSION\\$folder\\rebuild_index.sql\"" >> $DATAUPDATEBAT;
		fi
		if [ -f "$DATAUPDATEPATH/$folder/alter_procedure.sql" ];then 
			echo "found sql :$DATAUPDATEPATH/$folder/alter_procedure.sql";
			echo "mysql -h127.0.0.1 -ubxn -p{bxnPassword} --default-character-set=utf8 < \"{BX_HOME}\\data\\update\\$PACKAGEVERSION\\$folder\\alter_procedure.sql\"" >> $DATAUPDATEBAT;
		fi
	fi
done

# start bat
INSTALLSTARTBAT=$UPDATEBASE/bxn-update-base-start.bat;
echo "touch $INSTALLSTARTBAT";
touch "$INSTALLSTARTBAT";
if [ -f "$INSTALLSTARTBAT" ];then
	echo "touch $INSTALLSTARTBAT success!";
else
	echo "ERROR!! touch $INSTALLSTARTBAT failed!";
	exit -1;
fi
echo "cd bx_app\\installer" >> $INSTALLSTARTBAT;
echo "java -classpath bxn-update-base.jar; cn.boxiao.bxn.install.ApplicationLanuch" >> $INSTALLSTARTBAT;

# start jar and properties
INSTALLJAR=/var2/share/resource_library/11-公司产品安装包/bxn-install-jar;
INSTALLER=$UPDATEBASE/bx_app/installer;
echo "cp $INSTALLJAR/bxn-update-base.jar $INSTALLER/";
cp $INSTALLJAR/bxn-update-base.jar $INSTALLER/;
if [ -f "$INSTALLER/bxn-update-base.jar" ];then
	echo "cp $INSTALLER/bxn-update-base.jar success!";
else
	echo "ERROR!! cp $INSTALLER/bxn-update-base.jar failed!";
	exit -1;
fi
echo "cp $INSTALLJAR/bxn-update-base.properties $INSTALLER/";
cp $INSTALLJAR/bxn-update-base.properties $INSTALLER/;
if [ -f "$INSTALLER/bxn-update-base.properties" ];then
	echo "cp $INSTALLER/bxn-update-base.properties success!";
else
	echo "ERROR!! cp $INSTALLER/bxn-update-base.properties failed!";
	exit -1;
fi

# current version 
echo "echo version to $INSTALLER/bxn-update-base.properties";
echo "# current version"  >> $INSTALLER/bxn-update-base.properties;
echo "version.current=$PACKAGEVERSION" >> $INSTALLER/bxn-update-base.properties;


echo "zip begin";
cd $UPDATEBASE;
echo "zip -r $UPDATEZIP *";
zip -r $UPDATEZIP *
echo "mv $UPDATEZIP /opt/build/$TODAY/";
mv $UPDATEZIP /opt/build/$TODAY/
rm -rf $UPDATEBASE
echo "$(date +%Y%m%d%k%M%S)" package end.
